#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../include/main.h"
void createFileVfs(char name[],long int size)
{
	//char file1[20];	
	FILE *fp;
	int i,size1;
	char x[30],l;
	//printf("ENter file name:");
	//scanf("%s",file1);
	struct header h;
	struct fileDescriptor fd;
	struct freeList fl;	
	struct Block b;
	size =size*1024;
	int n = (size-sizeof(h)) / (sizeof(fd)+sizeof(fl)+sizeof(b));
	//printf("Number of blocks :: %d",n);
	
	fp=fopen(name,"wb+");
	//writing meta
	h.maxfd=n;
	//printf("Number of blocks created: %d\n",n);
	strcpy(h.fileSystemLabel,name);
	fwrite(&h,sizeof(h),1, fp );
//-------------------------------------------------------	
	//writing freelist	
	//fl.
	for(i=0;i<n;i++)
	{	fl.freel='0';
	//fseek(fp,i*1024,SEEK_SET);
	fwrite(&fl,sizeof(fl),1, fp );
	
	}
//-----------------------------------------------------------
	//writing file descriptors

	
	for(i=0;i<n;i++)
	{	fd.isfull='0';					
	//fseek(fp,i*1024,SEEK_SET);
	fwrite(&fd,sizeof(fd),1, fp );
	
	}

		

//-----------------------------------------------------------		
	//writing blocks
	for(i=0;i<n;i++)
	{	
	b.isfull='0';
	//fseek(fp,i*1024,SEEK_SET);
	fwrite(&b,sizeof(b),1, fp );
	
	}
	char padding;
	fwrite(&padding,sizeof(padding),(size-ftell(fp)), fp );
//--------------------------------------------------------------
	

//Creating Root and Writing to file system
		//rewind(fp);
		size1=sizeof(struct header)+n*sizeof(struct freeList);
		fseek(fp,size1,SEEK_SET);
	
	        strcpy(fd.fileName ,"/");
		strcpy(fd.locationPath ,"/");
		strcpy(fd.fileType ,"dir");
		fd.fileSize = 0;
		fd.blockNumber = -1;
		fd.isfull='1';
		fwrite(&fd,sizeof(fd),1, fp );

	//fclose(fp);			
	rewind(fp);
	//fwrite(name,50,1,fp);
	//rewind(fp);
	fread(x,20,1,fp);

	//printf("\nFile system name:%s\n",x);
	rewind(fp);
	//printf("%d\n",sizeof(h));
	//printf("%d\n",sizeof(fl));
	fseek(fp,sizeof(struct header),SEEK_SET);
	//printf("Flist-> ");
	for(i=0;i<n;i++)
	{
		fread(&fl,sizeof(struct freeList),1,fp);
		//printf("%c",fl.freel);
	}
	//printf("\n");
	//printf("FDesc->	");
	fread(&fd,sizeof(struct fileDescriptor),1,fp);
	//printf("%c",fd.isfull);
	//printf("%s\t",fd.fileName);
	
	for(i=1;i<n;i++)
	{
		fread(&fd,sizeof(struct fileDescriptor),1,fp);
		//printf("%c",fd.isfull);
		
	}	
	//printf("\n");
	//printf("Block->	");
	for(i=0;i<n;i++)
	{
		fread(&b,sizeof(struct Block),1,fp);
		//printf("%c",b.isfull);
	}
	fclose(fp);
	//printf("file system created\n");		

	/*fclose(fp);
	fp=fopen(file1,"rb");
	fread( x, 5, 1, fp );
	printf("%s",x);
	fclose(fp);*/
	

}
